using Microsoft.Office.Interop.Excel;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace NoteManger
{
	public class FrmDownLoadNote : Form
	{
		private IContainer components = null;
		private System.Windows.Forms.Label label1;
		private DateTimePicker dtpbegindate;
		private DateTimePicker dtpenddate;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnDownLoad;
		private RadioButton rbtnlocalmdb;
		private RadioButton rbtnexcel;
		private RichTextBox rtboxinfo;
		private System.Windows.Forms.Label label3;
		private NoteRemote _NoteRemoteObject = null;
		private TLoginInfo _LoginInfo = new TLoginInfo();
		public NoteRemote NoteRemoteObject
		{
			set
			{
				this._NoteRemoteObject = value;
			}
		}
		public TLoginInfo LoginInfo
		{
			set
			{
				this._LoginInfo = value;
			}
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.dtpbegindate = new DateTimePicker();
			this.dtpenddate = new DateTimePicker();
			this.label2 = new System.Windows.Forms.Label();
			this.btnDownLoad = new System.Windows.Forms.Button();
			this.rbtnlocalmdb = new RadioButton();
			this.rbtnexcel = new RadioButton();
			this.rtboxinfo = new RichTextBox();
			this.label3 = new System.Windows.Forms.Label();
			base.SuspendLayout();
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(24, 21);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(53, 12);
			this.label1.TabIndex = 0;
			this.label1.Text = "开始日期";
			this.dtpbegindate.Location = new System.Drawing.Point(83, 17);
			this.dtpbegindate.Name = "dtpbegindate";
			this.dtpbegindate.Size = new System.Drawing.Size(138, 21);
			this.dtpbegindate.TabIndex = 1;
			this.dtpenddate.Location = new System.Drawing.Point(286, 17);
			this.dtpenddate.Name = "dtpenddate";
			this.dtpenddate.Size = new System.Drawing.Size(138, 21);
			this.dtpenddate.TabIndex = 3;
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(227, 21);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(53, 12);
			this.label2.TabIndex = 2;
			this.label2.Text = "截止日期";
			this.btnDownLoad.Location = new System.Drawing.Point(26, 222);
			this.btnDownLoad.Name = "btnDownLoad";
			this.btnDownLoad.Size = new System.Drawing.Size(102, 23);
			this.btnDownLoad.TabIndex = 4;
			this.btnDownLoad.Text = "下载(&D)";
			this.btnDownLoad.UseVisualStyleBackColor = true;
			this.btnDownLoad.Click += new EventHandler(this.btnDownLoad_Click);
			this.rbtnlocalmdb.AutoSize = true;
			this.rbtnlocalmdb.Enabled = false;
			this.rbtnlocalmdb.Location = new System.Drawing.Point(345, 225);
			this.rbtnlocalmdb.Name = "rbtnlocalmdb";
			this.rbtnlocalmdb.Size = new System.Drawing.Size(83, 16);
			this.rbtnlocalmdb.TabIndex = 6;
			this.rbtnlocalmdb.TabStop = true;
			this.rbtnlocalmdb.Text = "本地数据库";
			this.rbtnlocalmdb.UseVisualStyleBackColor = true;
			this.rbtnlocalmdb.CheckedChanged += new EventHandler(this.rbtnlocalmdb_CheckedChanged);
			this.rbtnexcel.AutoSize = true;
			this.rbtnexcel.Checked = true;
			this.rbtnexcel.Location = new System.Drawing.Point(262, 225);
			this.rbtnexcel.Name = "rbtnexcel";
			this.rbtnexcel.Size = new System.Drawing.Size(77, 16);
			this.rbtnexcel.TabIndex = 7;
			this.rbtnexcel.TabStop = true;
			this.rbtnexcel.Text = "Excel文件";
			this.rbtnexcel.UseVisualStyleBackColor = true;
			this.rtboxinfo.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.rtboxinfo.Location = new System.Drawing.Point(26, 44);
			this.rtboxinfo.Name = "rtboxinfo";
			this.rtboxinfo.Size = new System.Drawing.Size(398, 171);
			this.rtboxinfo.TabIndex = 0;
			this.rtboxinfo.Text = "";
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(215, 227);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(41, 12);
			this.label3.TabIndex = 9;
			this.label3.Text = "保存为";
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new System.Drawing.Size(451, 251);
			base.Controls.Add(this.label3);
			base.Controls.Add(this.rtboxinfo);
			base.Controls.Add(this.rbtnexcel);
			base.Controls.Add(this.rbtnlocalmdb);
			base.Controls.Add(this.btnDownLoad);
			base.Controls.Add(this.dtpenddate);
			base.Controls.Add(this.label2);
			base.Controls.Add(this.dtpbegindate);
			base.Controls.Add(this.label1);
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "FrmDownLoadNote";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = "下载日志";
			base.ResumeLayout(false);
			base.PerformLayout();
		}
		public FrmDownLoadNote()
		{
			this.InitializeComponent();
		}
		private void btnDownLoad_Click(object sender, EventArgs e)
		{
			this.rtboxinfo.Clear();
			try
			{
				if (!this._NoteRemoteObject.ConnectionTest())
				{
					MessageBox.Show("远程连接已断开，请重新连接后再试！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					return;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message + "远程连接已断开，请重新连接后再试！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			string text = DateTime.Now.ToString("yyyy-MM-dd");
			text = text + " " + DateTime.Now.ToString("HH:mm:ss");
			text = text + ":" + DateTime.Now.Millisecond.ToString("000");
			text += " 开始获取要下载的日志总数......\n";
			this.rtboxinfo.AppendText(text);
			this.rtboxinfo.Focus();
			System.Windows.Forms.Application.DoEvents();
			int value = 0;
			string str = "";
			if (!this._NoteRemoteObject.GetDownLoadDetailNoteCount(this._LoginInfo.LoginID, this.dtpbegindate.Value.ToString("yyyy-MM-dd"), this.dtpenddate.Value.ToString("yyyy-MM-dd"), ref value, ref str))
			{
				text = DateTime.Now.ToString("yyyy-MM-dd");
				text = text + " " + DateTime.Now.ToString("HH:mm:ss");
				text = text + ":" + DateTime.Now.Millisecond.ToString("000");
				text = text + " " + str + "\n";
				this.rtboxinfo.AppendText(text);
				this.rtboxinfo.Focus();
				System.Windows.Forms.Application.DoEvents();
			}
			else
			{
				text = DateTime.Now.ToString("yyyy-MM-dd");
				text = text + " " + DateTime.Now.ToString("HH:mm:ss");
				text = text + ":" + DateTime.Now.Millisecond.ToString("000");
				text = text + " 要下载的日志总数为：" + Convert.ToString(value) + "\n";
				this.rtboxinfo.AppendText(text);
				this.rtboxinfo.Focus();
				System.Windows.Forms.Application.DoEvents();
				text = DateTime.Now.ToString("yyyy-MM-dd");
				text = text + " " + DateTime.Now.ToString("HH:mm:ss");
				text = text + ":" + DateTime.Now.Millisecond.ToString("000");
				text += " 开始下载所选日期区间的日志明细......\n";
				this.rtboxinfo.AppendText(text);
				this.rtboxinfo.Focus();
				System.Windows.Forms.Application.DoEvents();
				string[,] array = new string[0, 0];
				if (!this._NoteRemoteObject.DownLoadDetailNote(this._LoginInfo.LoginID, this.dtpbegindate.Value.ToString("yyyy-MM-dd"), this.dtpenddate.Value.ToString("yyyy-MM-dd"), ref array, ref str))
				{
					text = DateTime.Now.ToString("yyyy-MM-dd");
					text = text + " " + DateTime.Now.ToString("HH:mm:ss");
					text = text + ":" + DateTime.Now.Millisecond.ToString("000");
					text = text + " " + str + "\n";
					this.rtboxinfo.AppendText(text);
					this.rtboxinfo.Focus();
					System.Windows.Forms.Application.DoEvents();
				}
				else
				{
					if (array.Length == 0)
					{
						text = DateTime.Now.ToString("yyyy-MM-dd");
						text = text + " " + DateTime.Now.ToString("HH:mm:ss");
						text = text + ":" + DateTime.Now.Millisecond.ToString("000");
						text += " 程序异常！\n";
						this.rtboxinfo.AppendText(text);
						this.rtboxinfo.Focus();
						System.Windows.Forms.Application.DoEvents();
					}
					else
					{
						if (array.GetUpperBound(0) + array.GetUpperBound(1) + 1 == array.Length)
						{
							text = DateTime.Now.ToString("yyyy-MM-dd");
							text = text + " " + DateTime.Now.ToString("HH:mm:ss");
							text = text + ":" + DateTime.Now.Millisecond.ToString("000");
							text += " 所选日期区间没有日志明细！\n";
							this.rtboxinfo.AppendText(text);
							this.rtboxinfo.Focus();
							System.Windows.Forms.Application.DoEvents();
						}
						else
						{
							Microsoft.Office.Interop.Excel.Application application = null;
							_Worksheet worksheet = null;
							try
							{
								application = new ApplicationClass();
								_Workbook workbook = application.Workbooks.Add(Missing.Value);
								worksheet = (_Worksheet)workbook.ActiveSheet;
							}
							catch
							{
								text = DateTime.Now.ToString("yyyy-MM-dd");
								text = text + " " + DateTime.Now.ToString("HH:mm:ss");
								text = text + ":" + DateTime.Now.Millisecond.ToString("000");
								text += " 创建Excel进程出现错误，系统中没有安装合适版本的Microsoft Excel产品！\n";
								this.rtboxinfo.AppendText(text);
								this.rtboxinfo.Focus();
								System.Windows.Forms.Application.DoEvents();
								return;
							}
							text = DateTime.Now.ToString("yyyy-MM-dd");
							text = text + " " + DateTime.Now.ToString("HH:mm:ss");
							text = text + ":" + DateTime.Now.Millisecond.ToString("000");
							text += " 正在初始化日志内存表......\n";
							this.rtboxinfo.AppendText(text);
							this.rtboxinfo.Focus();
							System.Windows.Forms.Application.DoEvents();
							text = DateTime.Now.ToString("yyyy-MM-dd");
							text = text + " " + DateTime.Now.ToString("HH:mm:ss");
							text = text + ":" + DateTime.Now.Millisecond.ToString("000");
							text += " 正在请求Excel填充日志并显示......\n";
							this.rtboxinfo.AppendText(text);
							this.rtboxinfo.Focus();
							System.Windows.Forms.Application.DoEvents();
							Range range = worksheet.get_Range("A1", Type.Missing);
							range = range.get_Resize(array.GetUpperBound(0) + 1, array.GetUpperBound(1) + 1);
							range.Value2 = array;
							GC.Collect();
							text = DateTime.Now.ToString("yyyy-MM-dd");
							text = text + " " + DateTime.Now.ToString("HH:mm:ss");
							text = text + ":" + DateTime.Now.Millisecond.ToString("000");
							text += " 日志明细下载完毕！\n";
							this.rtboxinfo.AppendText(text);
							this.rtboxinfo.Focus();
							System.Windows.Forms.Application.DoEvents();
							MessageBox.Show("日志明细下载完毕！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
							application.Visible = true;
							application = null;
							worksheet = null;
						}
					}
				}
			}
		}
		private void rbtnlocalmdb_CheckedChanged(object sender, EventArgs e)
		{
		}
	}
}
